<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('公司名')" prop="companyName">
            <el-input v-model="form.companyName" :placeholder="$t('公司名')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('竞品名')" prop="jingpinProduct">
            <el-input v-model="form.jingpinProduct" :placeholder="$t('竞品名')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('产品')" prop="product">
            <el-input v-model="form.product" :placeholder="$t('产品')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资时间')" prop="setupDate">
            <el-date-picker
              v-model="form.setupDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('投资时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('时间')" prop="competitorDate">
            <el-date-picker
              v-model="form.competitorDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('地区')" prop="location">
            <el-input v-model="form.location" :placeholder="$t('地区')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('行业')" prop="industry">
            <el-input v-model="form.industry" :placeholder="$t('行业')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('估值(元)')" prop="competitorValue">
            <Money v-model="form.competitorValue" :placeholder="$t('估值')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('业务范围')" prop="business">
            <el-input v-model="form.business" :placeholder="$t('业务范围')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('logo')" prop="logoInfo">
            <!--            <el-input v-model="form.logoInfo" :placeholder="$t('logo')" show-word-limit />-->
            <UploadAvatar :logo-info="form.logoInfo" @appendData="uploadImg($event)" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="competitorRemark">
            <el-input v-model="form.competitorRemark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="company:competitor" />
    </div>
  </div>
</template>

<script>
    import { saveCompanyCompetitor, getCompanyCompetitor, uploadAvatar } from '@/api/company/competitor/companyCompetitor'
    import UploadAvatar from '@/components/Avatar/uploadAvatar'
    export default {
      components: {
        UploadAvatar
      },
        data() {
            return {
                // 表单参数
                form: {
                  ogoInfo: undefined
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                jingpinProduct: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                location: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                product: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                setupDate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ]
                }
            }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getCompanyCompetitor(data.id).then(response => {
                        this.form = response.data
                      if (this.form.competitorValue) {
                        this.form.competitorValue = Number(this.form.competitorValue)
                      }
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    companyId: this.$route.query.companyId,
                    companyName: undefined,
                    competitorDate: undefined,
                    industry: undefined,
                    logoInfo: undefined,
                    jingpinProduct: undefined,
                    location: undefined,
                    product: undefined,
                    setupDate: undefined,
                    competitorValue: undefined,
                    business: undefined,
                    competitorRemark: undefined,
                    delFlag: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveCompanyCompetitor(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                              if (this.$route.query.companyId) {
                                this.$store.dispatch('bussRisk/UPDATE_BUSS_RISK_COUNT', { id: this.$route.query.companyId })
                              }
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          // 上传图片
          uploadImg(data) {
            const formData = new FormData()
            formData.append('avatarfile', data)
            formData.append('id', this.form.id)
            uploadAvatar(formData).then(response => {
              if (response.code === 200) {
                this.form.logoInfo = response.msg
                this.msgSuccess(this.$t('上传成功'))
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
    }
</script>
